package lifeDomain;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import support.Variables;

import android.database.Cursor;
import android.util.Log;

public class AppFactory extends State {
	public AppFactory(){
		super();
		if(packageNames == null)
			packageNames = new Hashtable<String, Integer>();
	}
	private static Hashtable< String, Integer> packageNames;
	private static int autoIncKey=0;
	public AppFactory(int _code, int _state) {
		super(_code, _state);
		// TODO Auto-generated constructor stub
		if(packageNames == null)
			packageNames = new Hashtable<String, Integer>();
	}
	public State ToState(Cursor cursor, int index)
	{
		if(cursor.isNull(index))
		{
//			return null;
		}
		
		int status;
		String name = cursor.getString(index);
		
		if(name == null)
			name = " ";
		if(packageNames.containsKey(name))
			status = packageNames.get(name);
		else
		{
			packageNames.put(name, autoIncKey);
			status = autoIncKey;
			//warning !! overflow (conceptual)
			autoIncKey++;
		}
		Set(STATE_APP, status);
		return this;
	}
	public String toString()
	{
		String str="";
		if(packageNames.containsValue(value))
		{
			Set<Entry<String, Integer>> set = packageNames.entrySet();
	        Iterator<Entry<String, Integer>> it = set.iterator();
	        while (it.hasNext()) {
	        	Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) it.next();
	        	if((int)(entry.getValue()) == value)
	        	{
	        		str += entry.getKey().toString();
	        	}
	          Log.d(Variables.tag,entry.getKey().toString() + " " + entry.getValue().toString());
	          	
	        }
		}
		return str;
	}
	public static  void  Print()
	{
		Set<Entry<String, Integer>> set = packageNames.entrySet();
        Iterator<Entry<String, Integer>> it = set.iterator();
        while (it.hasNext()) {
          Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) it.next();
          Log.d(Variables.tag,entry.getKey().toString() + " " + entry.getValue().toString());
          	
        }
	}
}
